package com.sfh.sfhshopweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sfh.sfhshopweb.entity.Category;
import com.sfh.sfhshopweb.entity.Goods;
import com.sfh.sfhshopweb.entity.GoodsCategoryRel;
import com.sfh.sfhshopweb.entity.ToolsFile;
import com.sfh.sfhshopweb.mapper.CategoryMapper;
import com.sfh.sfhshopweb.mapper.GoodsCategoryRelMapper;
import com.sfh.sfhshopweb.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sfh.sfhshopweb.service.IGoodsService;
import com.sfh.sfhshopweb.service.IToolsFileService;
import com.sfh.sfhshopweb.utils.CosUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 目录表 服务实现类
 * </p>
 *
 * @author kcyu
 * @since 2024-06-13
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private IToolsFileService toolsFileService;

    @Autowired
    private GoodsCategoryRelMapper goodsCategoryRelMapper;

    @Autowired
    private IGoodsService goodsService;

    private void setPictureUrl(Category category) {
        if (Objects.nonNull(category.getCoverPicKey())) {
            ToolsFile file = toolsFileService.getById(category.getCoverPicKey());
            if(Objects.isNull(file)) {
                return;
            }
            String Url = CosUtils.getEncodeUrl(file.getFilePath()).toString();
            category.setCoverPicUrl(Url);
        }
    }

    @Override
    public List<Category> getTopCategory() {
        return categoryMapper.selectList(new QueryWrapper<Category>().eq("level", "1"));
    }

    @Override
    public List<Category> getCategoryTree() {
        // 获取一级目录
        List<Category> topCategory = getTopCategory();
        // 获取二级目录
        for (Category category : topCategory) {
            List<Category> children = categoryMapper.selectList(new QueryWrapper<Category>().eq("parent_id", category.getId()));

            for (Category child : children) {
                setPictureUrl(child);
            }

            category.setChildren(children);
            setPictureUrl(category);
        }
        return topCategory;
    }

    @Override
    public Category getOne(Integer id) {
        Category one = categoryMapper.selectOne(new QueryWrapper<Category>().eq("id", id));
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", one.getId());
        List<Category> children = categoryMapper.selectList(wrapper);
        children.forEach(this::setPictureUrl);
        one.setChildren(children);
        return one;
    }

    @Override
    public List<Goods> getGoodsByCategoryId(Integer categoryId) {
        List<Goods> goodsByCategoryId = goodsCategoryRelMapper.getGoodsByCategoryId(categoryId);
        goodsByCategoryId.forEach(each -> {
            each.setCoverPic(goodsService.getGoodsCoverPic(each.getPicKey()));
        });
        return goodsByCategoryId;
    }
}
